import {
  addSubscribe
} from "../../services/visited/visited";

import Toast from "tdesign-miniprogram/toast/index";
import Message from 'tdesign-miniprogram/message/index';

Component({
  options: {
    addGlobalClass: true,
  },

  properties: {
    id: {
      type: Number,
      observer(id) {
        console.log("传递进来的id");
        console.log(id);
        this.genIndependentID(id);
        if (this.properties.thresholds?.length) {
          this.createIntersectionObserverHandle();
        }
      },
    },
    data: {
      type: Object,
      observer(data) {
        console.log("传递了data");
        if (!data) {
          return;
        }
        this.setData({
          article: data
        });
      },
    }
  },

  data: {
    // 这里是文章的一些信息。包括标签，还有
    article: {
      id: '',
      title: "",
      content: "",
      url: "",
      tags: "",
      total: 2
    },
    isValidityLinePrice: false,
  },

  lifetimes: {
    ready() {
      // console.log("打印了id");
      // console.log(this.properties.id);
      // console.log(this.data.article.id);
      // this.init();
    },
    detached() {
      this.clear();
    },
  },

  pageLifeTimes: {},

  methods: {

    addItem(e) {
      var that =this
      console.log(this.data.article.id);
      addSubscribe({
        id: this.data.article.id
      }).then(res => {
        console.log(res);
        Message.success({
          context: this,
          offset: [20, 32],
          duration: 3000,
          content: res.data.data,
        });
      }).catch(error => {
        Message.console.error();({
          context: this,
          offset: [20, 32],
          duration: 3000,
          content: error,
        });
      })
    },
    clickHandle() {
      this.triggerEvent('click', {
        goods: this.data.goods
      });
    },

    clickThumbHandle() {
      this.triggerEvent('thumb', {
        goods: this.data.goods
      });
    },

    // 这里是点击添加学习记录
    addCartHandle(e) {
      const {
        id
      } = e.currentTarget;
      const {
        id: cardID
      } = e.currentTarget.dataset;
      // 出发添加
      this.triggerEvent('add-cart', {
        ...e.detail,
        id,
        cardID,
        goods: this.data.goods,
      });
    },

    genIndependentID(id) {
      let independentID;
      if (id) {
        independentID = id;
      } else {
        independentID = `goods-card-${~~(Math.random() * 10 ** 8)}`;
      }
      this.setData({
        independentID
      });
    },

    init() {
      // const {
      //   thresholds,
      //   id
      // } = this.properties;
      // console.log("init");
      // console.log(id);
      // this.genIndependentID(id);
      // if (thresholds && thresholds.length) {
      //   this.createIntersectionObserverHandle();
      // }
    },

    clear() {
      this.clearIntersectionObserverHandle();
    },

    intersectionObserverContext: null,

    createIntersectionObserverHandle() {
      if (this.intersectionObserverContext || !this.data.independentID) {
        return;
      }
      this.intersectionObserverContext = this.createIntersectionObserver({
        thresholds: this.properties.thresholds,
      }).relativeToViewport();

      this.intersectionObserverContext.observe(
        `#${this.data.independentID}`,
        (res) => {
          this.intersectionObserverCB(res);
        },
      );
    },

    intersectionObserverCB() {
      this.triggerEvent('ob', {
        goods: this.data.goods,
        context: this.intersectionObserverContext,
      });
    },

    clearIntersectionObserverHandle() {
      if (this.intersectionObserverContext) {
        try {
          this.intersectionObserverContext.disconnect();
        } catch (e) {}
        this.intersectionObserverContext = null;
      }
    },
  },
});